<template>
  <div>

    <div class="table">
      <el-table :data="tableData" stripe >
        <el-table-column type="index" :index="hIndex" label="序号" width="70"></el-table-column>
        <el-table-column prop="cause" label="报销原因" show-overflow-tooltip></el-table-column>
        <el-table-column prop="createTime" label="申请时间"></el-table-column>
        <el-table-column prop="expensesType" label="报销类型"></el-table-column>
        <el-table-column prop="status" label="报销状态"></el-table-column>
        <el-table-column prop="totalAmount" label="报销金额"></el-table-column>
        <el-table-column label="查看" width="180" align="center">
          <template v-slot="scope">
            <el-button plain type="primary" @click="getExpensesInfo(scope.row.id)" size="mini">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template v-slot="scope">
            <el-button plain type="primary" v-if="scope.row.status === '审核不通过'" @click="handleEdit(scope.row)" size="mini">编辑</el-button>
            <el-button plain type="danger" size="mini" @click=del(scope.row.id) :disabled="scope.row.status !== '审核中'">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
            background
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[5, 10, 20]"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total">
        </el-pagination>
      </div>
    </div>

  </div>
</template>

<script>
export default {
  name: "ExpensesPerson",
  data() {
    return {
      tableData: [],  // 所有的数据
      form: {},
      pageNum: 1,   // 当前的页码
      pageSize: 10,  // 每页显示的个数
      total: 0,
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
    }
  },
  created() {
    this.load(1)
  },
  methods: {
    getExpensesInfo(id) {
      this.$router.push('/expensesInfo?expensesId='+id)
    },
    handleEdit(row) {   // 编辑数据
      this.$router.push('/expenses?expensesId='+row.id)  // 跳转主页
    },
    del(id) {   // 单个删除
      this.$confirm('您确定删除吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/expenses/delete/' + id).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    load(pageNum) {  // 分页查询
      if (pageNum) this.pageNum = pageNum
      this.$request.get('/expenses/getExpensesById', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          title: this.title,
        }
      }).then(res => {
        this.tableData = res.data?.list
        this.total = res.data?.total
      })
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },
    // 当前页数 - 1 * 每页数据条数 + index + 1 （ index 是索引值，从0开始）
    hIndex(index) {
      return (this.pageNum - 1) * this.pageSize + index + 1;
    }
  },

}
</script>

<style scoped>

</style>
